import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { Observable, throwError } from 'rxjs'
import { catchError } from 'rxjs/operators'

import { TerminalService } from './terminal.abstract.service'
import {
  ITermPagination,
  Terminal,
  ITerminalQueryParams,
  TerminalNewRequest,
  ITerminalUpdateRequest,
  ITerminalReportPagination,
} from 'app/shared/interfaces/terminal'
import { COMB_URL } from 'app/utils/contraints'
import { DataService } from '@shared/services/data.service'
import { preProcessParams, encodeParams } from '@shared/decorators'

@Injectable()
export class TerminalAlinterService implements TerminalService {
  constructor(private http: HttpClient, private dataService: DataService) {}

  /** 新接口 */
  @preProcessParams()
  @encodeParams()
  getList(params: ITerminalQueryParams, str?: string): Observable<ITermPagination> {
    // const options = generateOptions(params)
    const url = `${COMB_URL}/camel/terminals?${str}`
    return this.http.get<ITermPagination>(url).pipe(catchError(err => throwError(err)))
  }

  /** 新接口 */
  getOne(intStoreCode: string, termCode: string): Observable<Terminal> {
    const url = `${COMB_URL}/camel/store/${intStoreCode}/terminal/${termCode}`
    return this.http.get<Terminal>(url, { withCredentials: true }).pipe(catchError(err => throwError(err)))
  }

  /* 更新终端 */
  update(data: ITerminalUpdateRequest, intStoreCode: string, termCode: string): Observable<Terminal> {
    const url = `${COMB_URL}/camel/store/${intStoreCode}/terminal/${termCode}`
    return this.http.put<Terminal>(url, data).pipe(catchError(err => throwError(err)))
  }

  /** 创建终端 */
  createTerminal(data: TerminalNewRequest): Observable<boolean> {
    const url = `${COMB_URL}/camel/terminal`
    return this.http.post<boolean>(url, data).pipe(catchError(err => throwError(err)))
  }

  /** 删除终端  */
  deleteTerminal(intStoreCode: string, termCode: string): Observable<boolean> {
    const url = `${COMB_URL}/camel/store/${intStoreCode}/terminal/${termCode}`
    return this.http.delete<boolean>(url).pipe(catchError(err => throwError(err)))
  }

  /** 导出终端列表 */
  @preProcessParams()
  @encodeParams()
  getTerminalReport(params: ITerminalQueryParams, str?: string): Observable<ITerminalReportPagination> {
    const url = `${COMB_URL}/camel/terminals/report?${str}`
    return this.http.get<ITerminalReportPagination>(url)
  }

  /** 刷新激活码 */
  refreshCdKey(intStoreCode: string, termCode: string): Observable<string> {
    const url = `${COMB_URL}/camel/store/${intStoreCode}/terminal/${termCode}/cdkey`
    return this.http.put<string>(url, {})
  }

  /** 冻结 */
  freeze(intStoreCode: string, termCode): Observable<boolean> {
    const url = `${COMB_URL}/camel/store/${intStoreCode}/terminal/${termCode}/freeze`
    return this.http.put<boolean>(url, {})
  }

  /** 解冻 */
  unfreeze(intStoreCode: string, termCode: string): Observable<boolean> {
    const url = `${COMB_URL}/camel/store/${intStoreCode}/terminal/${termCode}/unfreeze`
    return this.http.put<boolean>(url, {})
  }
}
